<template>
	<view class="body">
		<xk-header :headerbg="true" :back="true" title="公司信息绑定"></xk-header>
		<view class="list-info">
			<view class="list-item">
				<view>公司名称</view>
				<input class="input-text" v-model="company_name" type="text" placeholder="请输入公司名称" placeholder-style="color:#ccc"/>
				<view @click="openInvoice" class="min-btn">发票抬头</view>
				<!-- <uni-icons @click="openInvoice" type="list" size="30" color="#2C72FF"></uni-icons> -->
			</view>
			<view class="list-item">
				<view>社会信用代码</view>
				<input class="input-text" v-model="company_social_code" maxlength="18" type="text" placeholder="请输入公司社会信用代码" placeholder-style="color:#ccc"/>
			</view>
			<view class="list-item">
				<view>姓名</view>
				<input class="input-text" v-model="user_name" type="text" placeholder="请输入您的姓名" placeholder-style="color:#ccc"/>
			</view>
			<view class="list-item" v-if="isCompayRegisger">
				<view>邀请码</view>
				<input class="input-text" v-model="invite_code" type="text" placeholder="请输入公司邀请码" placeholder-style="color:#ccc"/>
			</view>
		</view>

		<view class="common-btn" @click="bind()">确认绑定</view>
	</view>
</template>
<script>
	import config from '@/common/config.js'
	import moment from 'moment'

	export default {
		data() {
			return {
				isCompayRegisger: false,
				loginInfo: {},
				company_name: "",
				company_social_code: "",
				user_name: "",
				invite_code: "",

				notNull: {
					company_name: "请输入公司名称",
					company_social_code: "请输入公司社会信用代码",
					user_name: "请输入您的姓名"
				}
			};
		},
		onLoad(options) {
			if (options && options.loginInfo) {
				this.loginInfo = JSON.parse(options.loginInfo)
			}
		},
		methods:{
			openInvoice(){
				uni.chooseInvoiceTitle({
				    success: (res)=> {
						this.company_name = res.title;
						this.company_social_code = res.taxNumber
				        // console.log(res.type);
				        // console.log(res.title);
				        // console.log(res.taxNumber);
				        // console.log(res.companyAddress);
				        // console.log(res.telephone);
				        // console.log(res.bankName);
				        // console.log(res.bankAccount);
				  }
				})

			},
			bind() {
				for (let item in this.notNull) {
					if (!this[item]) {
						this.$util.toast(this.notNull[item]);
						return;
					}
				}
				if (!this.loginInfo.token) {
					this.$util.toast("请先登录");
					return
				}
				uni.requestSubscribeMessage({
					tmplIds: ['Uz7zwNCyORPCN94w6-OpW_69Bjo45GnFZv3U4Zh3Beg'],
					complete: ()=>{
						
						// 临时保存登录信息，以便后续请求时有权限
						let loginInfoTemp = this.$util.getStorage("loginInfo")
						
						this.loginInfo.name = this.user_name
						this.$util.setStorage("loginInfo", this.loginInfo)
						
						let query = this.$util.formatGraphqlQuery([{
							fun: "compayInfo:register_getCustomerFile",
							params: {
								company_social_code: this.company_social_code
							},
							result: "customer_id, company_name, company_social_code"
						}])
						this.$util.postGraphql({query}, {
							isLoading: true
						},'wx').then((res)=>{
							console.warn("公司信息查询结果：", res)
							if (res.compayInfo &&
								res.compayInfo.customer_id) {	// 该公司已经注册
								this.isCompayRegisger = true
								this.bindCompay(res.compayInfo.customer_id)
							} else {	// 公司未注册
								this.registerCompay()
							}
						})
						
						// 还原登录信息
						this.$util.setStorage("loginInfo", loginInfoTemp)
					}
				})
				
			},
			bindCompay(customer_id) {
				if (!this.invite_code) {
					this.$util.toast("公司已存在，请输入公司邀请码")
					return
				}
				// 临时保存登录信息，以便后续请求时有权限
				let loginInfoTemp = this.$util.getStorage("loginInfo")
				this.$util.setStorage("loginInfo", this.loginInfo)
				this.$util.post({
					url: "wx/register/bindCustomer",
					data: {
						customer_id: customer_id,
						user_name: this.user_name,
						invite_code: this.invite_code
					}
				}).then((res)=>{
					console.warn("公司信息绑定成功结果：", res)
					this.$util.toast("公司信息绑定成功");
					this.$util.reLaunchHomePage(res);
				});

				// 还原登录信息
				this.$util.setStorage("loginInfo", loginInfoTemp)
			},
			registerCompay() {
				// 临时保存登录信息，以便后续请求时有权限
				let loginInfoTemp = this.$util.getStorage("loginInfo")
				this.$util.setStorage("loginInfo", this.loginInfo)
				this.$util.post({
					url: "wx/register",
					data: {
						company_name: this.company_name,
						company_social_code: this.company_social_code,
						user_name: this.user_name
					}
				}).then((res)=>{
					console.warn("公司信息注册成功结果：", res)
					this.$util.toast("公司信息绑定成功");
					this.$util.reLaunchHomePage(res);
				});

				// 还原登录信息
				this.$util.setStorage("loginInfo", loginInfoTemp)
			}
		}
	}
</script>

<style lang="scss" scoped>
.min-btn{
	color: #2C72FF;
	font-size: 26rpx;
	padding: 10rpx 20rpx;
	border-radius: 10rpx;
	background-color: rgba($color: #2C72FF, $alpha: 0.1);

}
.list-info {
	margin: 20rpx 20rpx 0rpx 20rpx;

	.list-item {
		display: flex;
		background-color: #FFFFFF;
		border-radius: 10rpx;
		font-size: 28rpx;
		padding: 30rpx;
		font-size: 28rpx;
		color: #333;
		align-items: center;

		.input-text {
			flex: 1;
			color: #333;
			margin-left: 20rpx;
		}
	}
}
// .button {
// 	background-size:100%;
// 	background-image:url("/static/weixin.png") ;
// 	width: 100rpx;
// 	height: 100rpx;
// }
</style>
